package cn.com.smartdevices.bracelet.config;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.goodving.guli.activity.AppConfig;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.server.GuliHttpClient;
import com.goodving.guli.server.ClientDataConstants;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

import cn.com.smartdevices.bracelet.config.p049a.C0810a;
import cn.com.smartdevices.bracelet.config.p049a.C0811b;
import cn.com.smartdevices.bracelet.config.p049a.C0812c;
import cn.com.smartdevices.bracelet.config.p049a.C0813d;
import cz.msebera.android.httpclient.Header;
import de.greenrobot.event.EventBus;

/* renamed from: cn.com.smartdevices.bracelet.config.b */
public class Config { // Config
    private static final transient String TAG = "Config";
    private static final transient String f4175D = "huami.health.getNewUrlConfig.json";
    private static final transient String f4176E = "hm_channel";
    private static final transient String f4177F = "hm_device_type";
    private static final transient int f4178G = 524288;
    private static final transient Gson mGson;
    private static transient String f4180J = null;
    private static transient int f4181K = 0;
    private static transient String f4182L = null;
    private static transient String f4183M = null;
    private static transient String f4184N = null;
    private static transient Config mConfig = null;
    private static File mFile = null;
    public static final transient String f4187a = "config.json";
    public static transient boolean f4188x;
    public final transient String f4189A;
    public final transient String f4190B;
    @SerializedName(value = "expireInterval")
    private Long f4191I;
    @SerializedName(value = "login")
    public final C0824k f4192b;
    @SerializedName(value = "partner")
    public final C0826m f4193c;
    @SerializedName(value = "push")
    public final C0827n f4194d;
    @SerializedName(value = "lab")
    public final C0810a f4195e;
    @SerializedName(value = "service")
    public final C0813d f4196f;
    @SerializedName(value = "runner")
    public final C0812c f4197g;
    @SerializedName(value = "mall")
    public final C0825l f4198h;
    @SerializedName(value = "game")
    public final C0820g f4199i;
    @SerializedName(value = "bluetooth")
    public final C0814a f4200j;
    @SerializedName(value = "log")
    public final C0823j f4201k;
    @SerializedName(value = "weight")
    public final C0832s f4202l;
    @SerializedName(value = "weather")
    public final C0831r f4203m;
    @SerializedName(value = "upgrade")
    public final C0830q f4204n;
    @SerializedName(value = "feature")
    public final C0819f f4205o;
    @SerializedName(value = "relation")
    public final C0811b f4206p;
    @SerializedName(value = "honor")
    public final C0822i f4207q;
    @SerializedName(value = "sleep_notify")
    public final C0829p f4208r;
    @SerializedName(value = "heartrate")
    public final C0821h f4209s;
    @SerializedName(value = "shoes")
    public final C0828o f4210t;
    @SerializedName(value = "agreement")
    public final String f4211u;
    @SerializedName(value = "agreement_global")
    public final String f4212v;
    @SerializedName(value = "global")
    public final Boolean f4213w;
    public final transient String f4214y;
    public final transient String f4215z;

    static {
        mGson = new GsonBuilder().create();// C2584r().m19594i();
        f4188x = false;
        //f4184N = "https://hm.goodving.com/";
        f4184N = AppConfig.URL_TEST_DOMAIN;
        mConfig = null;
        mFile = null;
    }

    class C0817d extends ContentObserver {
        private Context f4217a;

        public C0817d(Context context, Handler handler) {
            super(handler);
            this.f4217a = null;
            this.f4217a = context;
        }

        public void onChange(boolean z) {
            try {
                Config a = Config.m8857a(new FileInputStream(Config.mFile));
                if (a != null) {
                    Config.m8863a(C0886a.m9065b(this.f4217a));
                    Config.f4182L = C0886a.m9062a(this.f4217a);
                    GuliLog.m12356a(a.f4201k.f4235d.booleanValue(), a.f4201k.f4236e.booleanValue());
                    Config.m8869b(a);
                    if (Config.f4188x) {
                        Log.e(ConfigProvider.f4166a, "reload config success");
                    }
                } else if (Config.f4188x) {
                    Log.e(ConfigProvider.f4166a, "reload config error");
                }
            } catch (Throwable e) {
                if (Config.f4188x) {
                    Log.e(ConfigProvider.f4166a, "reload config error", e);
                }
            }
        }
    }

    private Config() {
        this.f4192b = new C0824k();
        this.f4193c = new C0826m();
        this.f4194d = new C0827n();
        this.f4195e = new C0810a();
        this.f4196f = new C0813d();
        this.f4197g = new C0812c();
        this.f4198h = new C0825l();
        this.f4199i = new C0820g();
        this.f4200j = new C0814a();
        this.f4201k = new C0823j();
        this.f4202l = new C0832s();
        this.f4203m = new C0831r();
        this.f4204n = new C0830q();
        this.f4205o = new C0819f();
        this.f4206p = new C0811b();
        this.f4207q = new C0822i();
        this.f4208r = new C0829p();
        this.f4209s = new C0821h();
        this.f4210t = new C0828o();
        this.f4191I = Long.valueOf(86400000);//C4125d.f20547h);
        this.f4211u = "https://hm.goodving.com/huami.health.agreement.do";
        this.f4212v = "https://hm.mi-ae.com/huami.health.agreement.do";
        this.f4213w = Boolean.valueOf(false);
        this.f4214y = "1.0";
        this.f4215z = "c18c24046606b2e084edd37f9fe9f94d";
        this.f4189A = "1uMqYWpHo3MoLH";
        this.f4190B = C4220a.bO;
    }

    public static Config m8857a(InputStream inputStream) {
        Config config = null;
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        char[] cArr = new char[512];
        StringBuilder stringBuilder = new StringBuilder();
        int read;
        try {
            do {
    
                read = inputStreamReader.read(cArr, 0, cArr.length);
                if (read > 0) {
                    stringBuilder.append(cArr, 0, read);
                    continue;
                }
            } while (read > 0);
        } catch (IOException e) {
            //TBD
        }
        if (stringBuilder.length() > 0) {
            config = mGson.fromJson(stringBuilder.toString(), Config.class);
        }
        try {
            inputStreamReader.close();
        } catch (IOException e) {
            if (f4188x) {
                Log.e(TAG, e.getMessage());
            }
        }
        return config;
    }

    public static String m8858a() {
        return f4180J;
    }

    static final class C0816c extends AsyncHttpResponseHandler {
        final /* synthetic */ Context f4216a;

        C0816c(Context context) {
            this.f4216a = context;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
            if (Config.f4188x) {
                String str = C3657a.f18328f;
                if (bArr != null) {
                    str = new String(bArr);
                }
                Log.e("Config", "Status:" + i + " | content:" + str, th);
            }
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            if (i == 200) {
                try {
                    JSONObject jSONObject = new JSONObject(new String(bArr));
                    if (jSONObject.optInt("code") == 1) {
                        String string = jSONObject.getString("data");
                        JSONObject b = Config.m8868b(this.f4216a);
                        jSONObject = new JSONObject(string);
                        if (b != null) {
                            Config.m8864a(b, jSONObject);
                        } else {
                            b = jSONObject;
                        }
                        Config.m8869b((Config) Config.mGson.fromJson(b.toString(), Config.class));
                    }
                } catch (Exception e) {
                    if (Config.f4188x) {
                        Log.e("Config", e.getMessage());
                    }
                }
            }
        }
    }

    public static void m8859a(Context context) {
        RequestParams a = ClientDataConstants.m11558a(C0886a.m9071f(context));
        GuliHttpClient.asyncHttpClient.post(f4184N + f4175D, a, new C0816c(context));
    }

    public static void m8860a(Context context, boolean z) throws Throwable {
        InputStream inputStream = null;
        Exception e;
        Config a = null;
        Throwable th;
        Config config = null;
        if (mConfig == null) {
            mConfig = new Config();
            mConfig.m8871c(context);
            synchronized (Config.class) {
                mFile = new File(context.getCacheDir(), f4187a);
                if (z) {
                    Config.m8862a(mFile);
                }
                Object obj;
                try {
                    if (!mFile.exists() || f4188x) {
                        obj = config;
                    } else if (mFile.length() > 524288) {
                        Config.m8862a(mFile);
                        //inputStream = config; //TBD

                    } else {
                        try {
                            inputStream = new FileInputStream(mFile);
                            try {
                                config = Config.m8857a(inputStream);
                            } catch (Exception e2) {
                                e = e2;
                                if (f4188x) {
                                    Log.e(TAG, e.getMessage());
                                }
                                Config.m8862a(mFile);
                                if (config == null) {
                                    try {
                                        inputStream = context.getResources().getAssets().open(f4187a);
                                        a = Config.m8857a(inputStream);
                                    } catch (Exception e3) {
                                        if (f4188x) {
                                            Log.e(TAG, e3.getMessage());
                                        }
                                    } catch (Throwable th2) {
                                        th = th2;
                                        if (inputStream != null) {
                                            try {
                                                inputStream.close();
                                            } catch (IOException e4) {
                                                if (f4188x) {
                                                    Log.e(TAG, e4.getMessage());
                                                }
                                            }
                                        }
                                        throw th;
                                    }
                                    if (inputStream != null) {
                                        try {
                                            inputStream.close();
                                        } catch (IOException e42) {
                                            if (f4188x) {
                                                Log.e(TAG, e42.getMessage());
                                            }
                                        }
                                    }
                                    if (a != null) {
                                        mConfig = a;
                                    }
                                }
                                a = config;
                                if (inputStream != null) {
                                    inputStream.close();
                                }
                                if (a != null) {
                                    mConfig = a;
                                }
                            }
                        } catch (Exception e5) {
                            Exception e3 = e5;
//                            inputStream = config;
                            if (f4188x) {
                                Log.e(TAG, e3.getMessage());
                            }
                            Config.m8862a(mFile);
                            if (config == null) {
                                inputStream = context.getResources().getAssets().open(f4187a);
                                a = Config.m8857a(inputStream);
                                if (inputStream != null) {
                                    inputStream.close();
                                }
                                if (a != null) {
                                    mConfig = a;
                                }
                            }
                            a = config;
                            if (inputStream != null) {
                                inputStream.close();
                            }
                            if (a != null) {
                                mConfig = a;
                            }
                        }
                    }
                    if (config == null) {
                        inputStream = context.getResources().getAssets().open(f4187a);
                        a = Config.m8857a(inputStream);
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        if (a != null) {
                            mConfig = a;
                        }
                    }
                    a = config;
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    if (a != null) {
                        mConfig = a;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    obj = config;
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e2) {
                            //TBD
                        }
                    }
                    throw th;
                }
            }
        }
    }

    private static void m8862a(File file) {
        if (file != null && file.exists() && !file.delete()) {
            file.deleteOnExit();
        }
    }

    static void m8863a(String str) {
        f4184N = str;
    }

    public static void m8864a(JSONObject jSONObject, JSONObject jSONObject2) {
        if (jSONObject2 != null) {
            Iterator keys = jSONObject2.keys();
            while (keys.hasNext()) {
                try {
                    String str = (String) keys.next();
                    Object obj = jSONObject2.get(str);
                    if (jSONObject.isNull(str)) {
                        jSONObject.put(str, obj);
                    } else if (obj instanceof JSONObject) {
                        Object obj2 = jSONObject.get(str);
                        if (obj2 instanceof JSONObject) {
                            Config.m8864a((JSONObject) obj2, (JSONObject) obj);
                        }
                    } else if (jSONObject.get(str).getClass() == obj.getClass()) {
                        jSONObject.put(str, obj);
                    }
                } catch (JSONException e1) {
                    //TBD
                }
            }
        }
    }

    public static int m8865b() {
        return f4181K;
    }

    private static String m8866b(InputStream inputStream) {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        char[] cArr = new char[512];
        try {
            String stringBuilder;
            StringBuilder stringBuilder2 = new StringBuilder();
            try {
                while (true) {
                    int read = inputStreamReader.read(cArr, 0, cArr.length);
                    if (read > 0) {
                        stringBuilder2.append(cArr, 0, read);
                        continue;
                    }
                    if (read <= 0) {
                        break;
                    }
                }
            } catch (IOException e) {
                //TBD
            }
            if (stringBuilder2.length() > 0) {
                stringBuilder = stringBuilder2.toString();
            } else {
                stringBuilder = null;
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    if (f4188x) {
                        Log.e(TAG, e.getMessage());
                    }
                }
            }
            return stringBuilder;
        } finally {
            try {
                inputStreamReader.close();
            } catch (IOException e2) {
                if (f4188x) {
                    Log.e(TAG, e2.getMessage());
                }
            }
        }
    }

    public static JSONObject m8868b(Context context) {
        Exception e;
        Throwable th;
        InputStream inputStream = null;
        InputStream open;
        try {
            String b = "";
            CharSequence b2 = (mFile == null || !mFile.exists()) ? null : Config.m8866b(new FileInputStream(mFile));
            if (TextUtils.isEmpty(b2)) {
                open = context.getResources().getAssets().open(f4187a);
                try {
                    b2 = Config.m8866b(open);
                } catch (Exception e2) {
                    e = e2;
                    try {
                        if (f4188x) {
                            Log.e(TAG, e.getMessage());
                        }
                        if (open != null) {
                            try {
                                open.close();
                            } catch (IOException e3) {
                                if (f4188x) {
                                    Log.e(TAG, e3.getMessage());
                                }
                            }
                        }
                        return null;
                    } catch (Throwable th2) {
                        th = th2;
                        inputStream = open;
                        if (inputStream != null) {
                            try {
                                inputStream.close();
                            } catch (IOException e4) {
                                if (f4188x) {
                                    Log.e(TAG, e4.getMessage());
                                }
                            }
                        }
                        throw th;
                    }
                }
            }
            open = null;
            if (TextUtils.isEmpty(b2)) {
                b = mGson.toJson(mConfig);
            } else {
                CharSequence charSequence = b2;
            }
            JSONObject jSONObject = new JSONObject(b);
            if (open == null) {
                return jSONObject;
            }
            try {
                open.close();
                return jSONObject;
            } catch (IOException e42) {
                if (!f4188x) {
                    return jSONObject;
                }
                Log.e(TAG, e42.getMessage());
                return jSONObject;
            }
        } catch (Exception e5) {
            e = e5;
            open = null;
            if (f4188x) {
                Log.e(TAG, e.getMessage());
            }
            if (open != null) {
                try {
                    open.close();
                } catch (IOException e99) {
                    //TBD
                }
            }
            return null;
        } catch (Throwable th3) {
            th = th3;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e99) {
                    //TBD
                }

            }
//            throw th;
        }
        return null;
    }

    private static void m8869b(Config config) {
        synchronized (Config.class) {
            if (config == null) {
                return;
            }
            mConfig = config;
            EventBus.getDefault().post(config);
            try {
                mConfig.m8881i();
            } catch (Throwable e) {
                //TBD
            }
            if (f4188x) {
                Log.d(TAG, "config load complete");
            }
        }
    }

    public static String m8870c() {
        return f4182L;
    }

    private void m8871c(Context context) {
        String str;
        int i;
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 16384);
            str = packageInfo.versionName;
            i = packageInfo.versionCode;
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageInfo.packageName, 128);
            if (applicationInfo != null) {
                Bundle bundle = applicationInfo.metaData;
                if (bundle != null) {
                    f4182L = bundle.getString(f4176E);
                    f4183M = bundle.getString(f4177F);
                }
            }
        } catch (NameNotFoundException e) {
            i = 0;
            str = C3657a.f18328f;
        }
        context.getContentResolver().registerContentObserver(ConfigProvider.f4168c, true, new C0817d(context, new Handler()));
        f4181K = i;
        f4180J = str;
        String b = C0886a.m9065b(context);
        if (TextUtils.isEmpty(b)) {
            f4184N = AppConfig.URL_TEST_DOMAIN;
        } else {
            f4184N = b;
        }
        if (TextUtils.isEmpty(f4183M)) {
            f4183M = "android_phone";
        }
        b = C0886a.m9062a(context);
        if (!TextUtils.isEmpty(b)) {
            f4182L = b;
        }
    }

    public static String m8872d() {
        return f4183M;
    }

    public static String m8873e() {
        return f4184N;
    }

    public static String m8874f() {
        return "https://hm.mi-ae.com/";
    }

    public static String m8875g() {
        return "http://hm-scale.app-xae.goodving.com/";
    }

    public static Config m8876h() {
        Config config;
        synchronized (Config.class) {
            config = mConfig;
        }
        return config;
    }

    public void m8879a(JSONObject jSONObject) {
        if (jSONObject != null) {
            Config.m8869b((Config) mGson.fromJson(jSONObject.toString(), Config.class));
        }
    }

    public void m8880b(JSONObject jSONObject) throws Throwable {
        BufferedOutputStream bufferedOutputStream;
        Throwable e;
        try {
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(mFile));
            try {
                bufferedOutputStream.write(jSONObject.toString().getBytes());
                if (bufferedOutputStream != null) {
                    try {
                        bufferedOutputStream.close();
                    } catch (Throwable e2) {
                        Log.e(TAG, "save file close failed", e2);
                    }
                }
            } catch (Exception e3) {
                Exception e2 = e3;
                try {
                    if (f4188x) {
                        Log.e(TAG, "config save file", e2);
                    }
                    if (bufferedOutputStream != null) {
                        try {
                            bufferedOutputStream.close();
                        } catch (Throwable e22) {
                            Log.e(TAG, "save file close failed", e22);
                        }
                    }
                } catch (Throwable th) {
                    Throwable e22 = th;
                    if (bufferedOutputStream != null) {
                        try {
                            bufferedOutputStream.close();
                        } catch (Throwable e4) {
                            Log.e(TAG, "save file close failed", e4);
                        }
                    }
                    throw e22;
                }
            }
        } catch (Exception e5) {
            Exception e22 = e5;
            bufferedOutputStream = null;
            if (f4188x) {
                Log.e(TAG, "config save file", e22);
            }
            if (bufferedOutputStream != null) {
                bufferedOutputStream.close();
            }
        } catch (Throwable th2) {
            Throwable e22 = th2;
            bufferedOutputStream = null;
            if (bufferedOutputStream != null) {
                bufferedOutputStream.close();
            }
            throw e22;
        }
    }

    public void m8881i() throws Throwable {
        Throwable e = new Throwable();
        if (mConfig == null) {
            Log.e(TAG, "config don't init");
            return;
        }
        BufferedOutputStream bufferedOutputStream;
        try {
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(mFile));
            try {
                bufferedOutputStream.write(mGson.toJson(mConfig).getBytes());
                if (bufferedOutputStream != null) {
                    try {
                        bufferedOutputStream.close();
                    } catch (Throwable e2) {
                        Log.e(TAG, "save file close failed", e2);
                    }
                }
            } catch (Exception e3) {
                Exception e2 = e3;
                try {
                    if (f4188x) {
                        Log.e(TAG, "config save file", e2);
                    }
                    if (bufferedOutputStream != null) {
                        try {
                            bufferedOutputStream.close();
                        } catch (Throwable e22) {
                            Log.e(TAG, "save file close failed", e22);
                        }
                    }
                } catch (Throwable th) {
                    Throwable e22 = th;
                    if (bufferedOutputStream != null) {
                        try {
                            bufferedOutputStream.close();
                        } catch (Throwable e4) {
                            Log.e(TAG, "save file close failed", e4);
                        }
                    }
                    throw e22;
                }
            }
        } catch (Exception e5) {
            Throwable e22 = e5;
            bufferedOutputStream = null;
            if (f4188x) {
                Log.e(TAG, "config save file", e22);
            }
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e100) {
                    //TBD
                }

            }
        } catch (Throwable th2) {
            Throwable e22 = th2;
            bufferedOutputStream = null;
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e100) {
                    //TBD
                }

            }
            throw e;
        }
    }
}
